# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=aegisub
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.4.1.r90.g18175e457
pkgrel=3
pkgdesc="Cross-platform advanced subtitle editor (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64 clang64 clangarm64')
url='https://aegisub.org/'
msys2_repository_url='https://github.com/TypesettingTools/Aegisub/'
msys2_references=(
  'archlinux: aegisub'
)
license=('spdx:BSD-3-Clause')
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost-libs"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-ffms2"
  "${MINGW_PACKAGE_PREFIX}-fftw"
  "${MINGW_PACKAGE_PREFIX}-gcc-libs"
  "${MINGW_PACKAGE_PREFIX}-hunspell"
  "${MINGW_PACKAGE_PREFIX}-icu"
  "${MINGW_PACKAGE_PREFIX}-libass"
  "${MINGW_PACKAGE_PREFIX}-libiconv"
  "${MINGW_PACKAGE_PREFIX}-openal"
  "${MINGW_PACKAGE_PREFIX}-portaudio"
  "${MINGW_PACKAGE_PREFIX}-uchardet"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-common-libs"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw-libs"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-boost"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-nasm"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw"
  "git"
)
options=('!emptydirs')
# Move to release tag after new release > 3.4.2
_commit="18175e4577277b1394f3abec1bd94eb914610b6c"
source=("${_realname}::git+https://github.com/TypesettingTools/Aegisub.git#commit=${_commit}"
        0001-workaround-try-except.patch
        0002-meson-remove-msvc-option.patch
        0003-fix-macro-for-mingw.patch
        0004-workaround-wxGlobalSEInformation-not-found.patch
        0005-append-manifest-for-mingw.patch
        0006-adapt-msys2-filesystem-layout.patch
        0007-workaround-build-error-with-libc-with-agi-fs-path.patch
        0008-correct-the-language-id-of-chinese.patch
        0009-subtitles_provider_csri-fix-type-mismatch-error.patch)
sha256sums=('9804c2d2c58b21c0573846c2659cb539c0a025fa9a2cd61e7fd2701dd4ab8df2'
            '1ae72647ceed8cfa0a870bf223fea98b29b234d0325db760883caa2ede3acc21'
            'ae1bff9b1d9e65717fb37dcb76a44023ce0c7dab99f15fd8126a4148ea196e2b'
            '5c3cc017b9c36c38bc7ec9243cb0478791a7d36e56fadf0af7f809c523744189'
            'f70c013bcddd7510b9685a730f3925e6dc26b3db4558ba92633fb48aec9153a1'
            '5d32a1556763c3cd919afa48fa58ff78129695efd31b088266a46b56d017a0be'
            'd5fd4bb8948d5d21b6a6db153e7971b7dd3b65d1f32b2cd7e263a528f343a0f3'
            'b22c4c71d1a723b213ac771926f6ab074d05fb3faf514340e3ccb0aa87734d39'
            '8a3083645eaee8ae3ff2658ab4ba694ab2e15274dfe482aa1b361c5fb36a17f0'
            '3545e009cc9c605749455170348095e4459979f4e7f0cf2e5e931a69ec500ea1')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd "${_realname}"
  git describe --long "${_commit}" | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${_realname}"

  meson subprojects download luajit
  meson subprojects packagefiles --apply luajit

  echo "#define BUILD_GIT_VERSION_NUMBER ${pkgver:0:5}" >> git_version.h
  echo "#define BUILD_GIT_VERSION_STRING \"${pkgver:0:5}\"" >> git_version.h
  echo '#define TAGGED_RELEASE 0' >> git_version.h
  echo '#define INSTALLER_VERSION "0.0.0"' >> git_version.h
  echo '#define RESOURCE_BASE_VERSION 0, 0, 0' >> git_version.h

  _apply_patch_with_msg \
    0001-workaround-try-except.patch \
    0002-meson-remove-msvc-option.patch \
    0003-fix-macro-for-mingw.patch \
    0004-workaround-wxGlobalSEInformation-not-found.patch \
    0005-append-manifest-for-mingw.patch \
    0006-adapt-msys2-filesystem-layout.patch

  # https://github.com/TypesettingTools/Aegisub/pull/366
  _apply_patch_with_msg \
    0007-workaround-build-error-with-libc-with-agi-fs-path.patch

  # https://github.com/TypesettingTools/Aegisub/pull/367
  _apply_patch_with_msg \
    0008-correct-the-language-id-of-chinese.patch \
    0009-subtitles_provider_csri-fix-type-mismatch-error.patch
}

build() {
  CFLAGS="-DUNICODE -D_UNICODE" \
  CXXFLAGS="-DUNICODE -D_UNICODE" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --buildtype=plain \
      -Dtests=false \
      "build-${MSYSTEM}" \
      "${_realname}"

  meson compile -C "build-${MSYSTEM}"
}

package() {
  meson install -C "build-${MSYSTEM}" --destdir "${pkgdir}"

  # remove conflict with luajit package
  rm -r "${pkgdir}${MINGW_PREFIX}"/{include,share,share/licenses}/luajit*
  rm "${pkgdir}${MINGW_PREFIX}"/lib/libluajit*
  rm "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/luajit*
  rm "${pkgdir}${MINGW_PREFIX}"/share/man/man1/luajit*

  install -Dm644 "build-${MSYSTEM}"/subprojects/csri/libcsri.dll "${pkgdir}${MINGW_PREFIX}"/bin

  install -Dm644 "${_realname}/LICENCE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENCE"
}
